﻿using System;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;

namespace SOFTECH.Controls
{
    public partial class Register : ModulePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Product);
            Response.Redirect(url);
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Components.Register register = new Components.Register();
                register.CompanyCode = txtCompanyCode.Text;
                register.CompanyName = txtCompanyName.Text;
                register.CompanyAddress = txtCompanyAddress.Text;
                register.CompanyPhone = txtCompanyPhone.Text;
                register.EmployeeFullName = txtEmployeeFullName.Text;
                register.EmployeeEmail = txtEmployeeEmail.Text;
                register.EmployeeID = txtEmployeeID.Text;
                register.EmployeeTitle = txtEmployeeTitle.Text;
                register.EmployeePhone = txtEmployeePhone.Text;
                register.Product = rblProduct.SelectedValue;

                register.Insert();

                User user = new User();
                user.Name = txtEmployeeFullName.Text;
                user.Email = txtEmployeeEmail.Text;
                user.Password = txtEmployeePassword.Text;
                int returnID = user.Register();
                if (returnID > 0)
                {
                    string code = EncryptHelper.Encrypt(txtEmployeeEmail.Text);
                    User.Active(code);

                    string url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Download);
                    Response.Redirect(url);
                }
                else if (returnID == -1)
                {
                    txtMessage.Text = "Email này đã đăng ký rồi. Vui lòng kiểm tra lại!";
                }
            }
        }
    }
}